package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplatecontract.LindiTradeApplicationTemplateContractQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationtemplatecontract.LindiTradeApplicationTemplateContractVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationTemplateContractFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.SafetyUtil;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 林地指标交易平台_交易申请合同模板控制器
 *
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_交易申请合同模板管理")
@RequestMapping("/basic/lindi/tradeApplicationTemplateContract")
public class LindiTradeApplicationTemplateContractController{

    @Autowired
    private LindiTradeApplicationTemplateContractFeignClient feignClient;

    @PostMapping("/addTradeApplicationTemplateContract")
    @Operation(summary = "添加交易申请合同模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:add")
    public R<?> addTradeApplicationTemplateContract(
            @RequestBody @Validated(Add.class)
            LindiTradeApplicationTemplateContractVO tradeApplicationTemplateContractVO){
        tradeApplicationTemplateContractVO.setCreateId(StpUtil.getLoginIdAsLong());
        tradeApplicationTemplateContractVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        tradeApplicationTemplateContractVO.setCreateTime(now);
        tradeApplicationTemplateContractVO.setUpdateTime(now);
        return feignClient.addTradeApplicationTemplateContract(tradeApplicationTemplateContractVO);
    }

    @DeleteMapping("/deleteTradeApplicationTemplateContractByTradeApplicationTemplateContractId")
    @Operation(summary = "删除交易申请合同模板 -> 根据交易申请合同模板ID删除")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:delete")
    public R<?> deleteTradeApplicationTemplateContractByTradeApplicationTemplateContractId(
            @RequestParam("contractTemplateId")
            @NotNull(message = "交易申请合同模板ID不能为空")
            @Parameter(name = "contractTemplateId", required = true, description = "交易申请合同模板ID")
            Long tradeApplicationTemplateContractId){
        return feignClient.deleteTradeApplicationTemplateContractByTradeApplicationTemplateContractId(
                tradeApplicationTemplateContractId);
    }

    @PutMapping("/updateTradeApplicationTemplateContractByTradeApplicationTemplateContractId")
    @Operation(summary = "修改交易申请合同模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:update")
    public R<?> updateTradeApplicationTemplateContractByTradeApplicationTemplateContractId(
            @RequestBody @Validated(Update.class) LindiTradeApplicationTemplateContractVO
                    tradeApplicationTemplateContractVO){
        tradeApplicationTemplateContractVO.setUpdateId(StpUtil.getLoginIdAsLong());
        tradeApplicationTemplateContractVO.setUpdateTime(LocalDateTime.now());
        return feignClient.updateTradeApplicationTemplateContractByTradeApplicationTemplateContractId(tradeApplicationTemplateContractVO);
    }

    @GetMapping("/getPageTradeApplicationTemplateContractList")
    @Operation(summary = "查询交易申请合同模板列表(分页)")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:get")
    public R<PageResp<LindiTradeApplicationTemplateContractVO>> getPageTradeApplicationTemplateContractList(
            LindiTradeApplicationTemplateContractQueryVO queryVO){
        return feignClient.getPageTradeApplicationTemplateContractList(queryVO);
    }

    @GetMapping("/getTradeApplicationTemplateContractByTradeApplicationTemplateContractId/{tradeApplicationTemplateContractId}")
    @Operation(summary = "根据交易申请合同模板ID查询交易申请合同模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:get")
    public R<LindiTradeApplicationTemplateContractVO> getTradeApplicationTemplateContractByTradeApplicationTemplateContractId(
            @PathVariable(value = "tradeApplicationTemplateContractId")
            @NotNull(message = "交易申请合同模板ID不能为空")
            @Parameter(name = "tradeApplicationTemplateContractId", description = "交易申请合同模板ID", required = true)
            Long tradeApplicationTemplateContractId){
        return feignClient.getTradeApplicationTemplateContractByTradeApplicationTemplateContractId(tradeApplicationTemplateContractId);
    }

    @GetMapping("/getContractTemplateList")
    @Operation(summary = "获取合同模板列表")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:get")
    public R<List<LindiContractTemplateVO>> getContractTemplateList(){
        return feignClient.getContractTemplateList();
    }

    @PostMapping("/generateTemplateContract")
    @Operation(summary = "生成交易申请合同模板")
    @SaCheckPermission("lindi:tradeApplicationTemplateContract:add")
    public R<?> generateTemplateContract(
            @RequestParam("tradeApplicationId") @NotNull(message = "交易申请ID不能为空")
            Long tradeApplicationId,
            @RequestParam("contractTemplateId") @NotNull(message = "合同模板ID不能为空")
            Long contractTemplateId,
            @RequestParam("contractType") @NotNull(message = "合同类型不能为空")
            String contractType){
        Long createId = StpUtil.getLoginIdAsLong();
        Long tenantId = SafetyUtil.getLoginUserTenantId();
        return feignClient.generateTemplateContract(tradeApplicationId
                , contractTemplateId, contractType, createId, tenantId);
    }
}